/*
 * TimeDamper.java
 *
 * Created on May 12, 2007, 5:52 PM
 *
 *  Life Logger
    Copyright (C) 2007  Anand Kishore (anand@semanticvoid.com)
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package com.semanticvoid.lifelogger.karma.search.factory.decorator;

import com.semanticvoid.lifelogger.configuration.ConfigManager;
import com.semanticvoid.lifelogger.entity.BrowseHistoryEntity;
import com.semanticvoid.lifelogger.entity.SearchHistoryEntity;
import com.semanticvoid.lifelogger.entity.WebHistoryEntity;
import com.semanticvoid.lifelogger.entity.comparator.DescendingScoreComparator;
import com.semanticvoid.lifelogger.karma.search.interfaces.ISearch;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Vector;

/**
 *
 * @author anand kishore
 */
public class TimeDamper implements ISearch {
    
    /** Searcher object */
    ISearch searcher;
    
    /** Creates a new instance of TimeDamper */
    public TimeDamper(ISearch searcher) {
        this.searcher = searcher;
    }
    
    
    public Vector<BrowseHistoryEntity> searchBrowseHistory(String query, short type, String fromDate, String toDate) {
        Vector<BrowseHistoryEntity> results = searcher.searchBrowseHistory(query, type, fromDate, toDate);
        
        for(BrowseHistoryEntity bEntity : results) {
            double score = bEntity.getScore();
            double tDamp = calculateTimeDamperForEntity(bEntity);
            bEntity.setScore(score * tDamp);
        }
        
        // Sort in descending order of score
        Collections.sort((List)results, new DescendingScoreComparator());
        
        return results;
    }
    
    public Vector<BrowseHistoryEntity> searchBrowseHistory(String query, short browseType) {
        return searchBrowseHistory(query, browseType, null, null);
    }
    
    public Vector<SearchHistoryEntity> searchSearchHistory(String query, String fromDate, String toDate) {
        Vector<SearchHistoryEntity> results = searcher.searchSearchHistory(query, fromDate, toDate);
        
        for(SearchHistoryEntity sEntity : results) {
            double score = sEntity.getScore();
            double tDamp = calculateTimeDamperForEntity(sEntity);
            sEntity.setScore(score * tDamp);
        }
        
        // Sort in descending order of score
        Collections.sort((List)results, new DescendingScoreComparator());
        
        return results;
    }

    public Vector<SearchHistoryEntity> searchSearchHistory(String query) {
        return searchSearchHistory(query, null, null);
    }
    
    private double calculateTimeDamperForEntity(WebHistoryEntity entity) {
        ConfigManager mgr = ConfigManager.getInstance();
        
        float tk = 0;
        float Is = 0;
        float k = 0;
        double tDamp;
        
        if(entity instanceof BrowseHistoryEntity) {
            tk = mgr.getTimeDamperTkForBrowse();
            Is = mgr.getTimeDamperIs();
            k = mgr.getTimeDamperKForBrowse();
        } else if(entity instanceof SearchHistoryEntity) {
            tk = mgr.getTimeDamperTkForSearch();
            Is = mgr.getTimeDamperIs();
            k = mgr.getTimeDamperKForSearch();
        }
        
        // Calculate dt
        long diffInMillis = new Date().getTime() - entity.getPubDate().getTime().getTime();
        long diffInHours = diffInMillis / (60 * 60 * 60);
        float dt = diffInHours / Is;
        
        // Calculate tk
        tk = tk / Is;
        
        tDamp = Math.pow(k, (dt/tk));
        
        return tDamp;
    }
}
